<template>
  <div class="container">
    <div class="title">
      <h2>活动管理</h2>
      <h5>用于管理小区里不定期举办的各种活动</h5>
    </div>
    <!-- 标题 -->
    <el-card class="box-card">
      <el-form :model="formInline" class="demo-form-inline">
        <el-form-item label="小区名称">
          <el-select v-model="formInline.community_code" placeholder="请输入">
            <el-option
              v-for="item in selectHome"
              :key="item.id"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="活动主题">
          <el-input v-model="formInline.title" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.status" placeholder="请输入">
            <el-option label="有效" value="1" />
            <el-option label="无效" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <div class="block">
            <el-date-picker
              v-model="createTime"
              style="width: 220px"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="timestamp"
              @change="changeTime"
            />
          </div>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button style="margin-left: 10px" @click="reSet">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格 -->
    <el-card class="tableCard">
      <el-button
        style="float: right; padding: 10px 20px; margin-right: 80px"
        type="primary"
        size="small"
        :disabled="!checkBtnPermission('/service/activity', 'create')"
        @click="drawer=true"
      >新增项目</el-button>
      <el-table :data="tableData">
        <el-table-column prop="id" label="ID" width="40%" />
        <el-table-column prop="community.name" label="小区名称" />
        <el-table-column prop="title" label="活动主题" />
        <el-table-column prop="place" label="活动地点" />
        <el-table-column label="照片">
          <template slot-scope="{ row }">
            <img v-imgerror="require('@/assets/images/isno.png')" :src="contenctPhoto(row.photo)[0]" style="height:80px" alt="" @click="showImg(contenctPhoto(row.photo))">
          </template>
        </el-table-column>
        <el-table-column prop="sponsor_unit" label="举办单位" />
        <el-table-column label="状态">
          <template slot-scope="{row}">
            {{ row.status ? '有效' : '无效' }}
          </template>
        </el-table-column>
        <el-table-column label="活动开始时间">
          <template slot-scope="{row}">
            {{ formatTimeThis(row.begin_time) }}
          </template>
        </el-table-column>
        <el-table-column label="活动结束时间">
          <template slot-scope="{row}">
            {{ formatTimeThis(row.end_time) }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="{row}">
            {{ formatTimeThis(row.create_time) }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="{row}">
            <el-button
              type="text"
              size="small"
              :disabled="!checkBtnPermission('/service/activity', 'update')"
              @click="edit(row.id)"
            >编辑</el-button>
            <el-button :disabled="!checkBtnPermission('/service/activity', 'destroy')" type="text" size="small" @click="delInfo(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" align="middle" style="float:right;padding: 10px 0 40px 0">
        <span>第 {{ startCount }}-{{ endCount }} 条/总共 {{ count }} 条</span>
        <el-pagination
          :current-page.sync="formInline.current"
          :page-size="formInline.pageSize"
          layout="prev, pager, next"
          :total="count"
          @current-change="handleCurrentChange"
        />
      </el-row>

    </el-card>
    <addDialog
      ref="addDialog"
      :drawer="drawer"
      :select-home="selectHome"
      :seeimage1="seeImage"
      @closeDrawer="closeDrawer"
      @getData="initData"
    />
  </div>
</template>

<script>
import addDialog from './components/addDialog.vue'
import { getSelectHome, getInitData, delData, getDetailData } from '@/api/service'
import { parseTime } from '@/filters/index'
// import { computed } from 'vue'
export default {
  name: 'VueAdminTemplateMasterProject',
  components: { addDialog },

  data() {
    return {
      drawer: false,
      count: null,
      createTime: [],
      formInline: {
        current: 1, // 页数
        pageSize: 3, // 每页数量
        community_code: '',
        title: '',
        status: null,
        create_time: []
      },
      tableData: [],
      selectHome: [],
      seeImage: []
    }
  },
  computed: {
    startCount() {
      if (this.tableData.length) {
        return this.formInline.current === 1 ? this.formInline.current : (this.formInline.current - 1) * this.formInline.pageSize + 1
      }
      return 0
    },
    endCount() {
      if (this.tableData.length) {
        return this.formInline.current === 1 ? this.tableData.length : (this.formInline.current - 1) * this.formInline.pageSize + this.tableData.length
      }
      return 0
    }
  },
  mounted() {
  },
  created() {
    this.initData()

    this.getSelectHome()
  },
  methods: {
    showImg(viewImgList) {
      this.$hevueImgPreview({
        multiple: true,
        nowImgIndex: 0,
        imgList: viewImgList
      })
    },
    // 关闭弹框
    closeDrawer() {
      this.seeImage = []
      this.drawer = false
    },
    // 存时间
    changeTime() {
      this.formInline.create_time = this.createTime.join(',')
    },
    // 获得小区
    async getSelectHome() {
      const res = await getSelectHome()
      this.selectHome = res.data.data
    },
    async initData() {
      const res = await getInitData(this.formInline)
      this.count = res.data.data.count
      this.tableData = res.data.data.rows
    },
    // 显示时间，转换时间
    formatTimeThis(time) {
      return parseTime(time)
    },
    handleCurrentChange(newPage) {
      this.formInline.current = newPage
      this.initData()
    },
    onSubmit() {
      // console.log(this.formInline)
      this.initData()
    },
    reSet() {
      this.formInline = {
        current: 1, // 页数
        pageSize: 3, // 每页数量
        community_code: '',
        title: '',
        status: null,
        create_time: []
      }
      this.initData()
    },
    async delInfo(id) {
      await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delData(id)
      if (this.formInline.current > 1 && this.tableData.length === 1) {
        this.formInline.current--
      }
      this.initData()
    },
    async edit(id) {
      const res = await getDetailData(id)
      console.log(res)
      this.$refs.addDialog.form = res.data.data
      const val = (this.$refs.addDialog.form.photo || '').split(',')
      this.$refs.addDialog.imgList = val
      val.forEach(item => {
        this.seeImage.push({ url: 'http://124.223.14.236:8881' + item })
      })

      this.drawer = true
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 40px;
  .title {
    display: flex;
    align-items: center;
    h2 {
      margin-right: 20px;
    }
    h5 {
      font-weight: 400;
      color: #9c9c9d;
      vertical-align: middle;
    }
  }
  .demo-form-inline {
    display: flex;
  }
  .el-form-item {
    margin-right: 22px;
  }

  .btn {
    margin-left: 265px;
    // float: right;
    padding-top: 40px;
    // margin-right: 10px;
    ::v-deep .el-form-item__content {
      display: flex;
    }
  }
  .box-card {
    margin-bottom: 10px;
  }
  ::v-deep .el-table__row{
    height: 100px;
  }
}
</style>

